﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string uid, pwd, cod, refer, icod;
            uid = Request.Form["uid"];
            pwd = Request.Form["pwd"];
            cod = Request.Form["cod"];
            refer = Request.Form["ref"];
            icod = (string)Session["ImgCode"];
            if (cod == null || icod==null || cod.ToLower()!=icod || cod.Length != 4)
            {
                Response.Write("<script>parent.set_dialog_login_error('·验证码错误!');</script>");
                Response.End();
            }
            if (uid == null || uid.Trim().Length ==0)
            {
                Response.Write("<script>parent.set_dialog_login_error('·请输入工号!');</script>");
                Response.End();
            }
            if (pwd == null || pwd.Trim().Length == 0)
            {
                Response.Write("<script>parent.set_dialog_login_error('·请输入密码!');</script>");
                Response.End();
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "CheckLogin";
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.Parameters.Add(new SqlParameter("@工号", SqlDbType.VarChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, uid));
            Cmd.Parameters.Add(new SqlParameter("@密码", SqlDbType.VarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, pwd));

            SqlDataReader Dr = Cmd.ExecuteReader();

            if (Dr.Read())
            {
                Session["OperatorID"] = uid.ToLower();
                Session["OperatorName"] = (string)Dr["姓名"];
                Session["OperatorDID"] = (int)Dr["部门ID"];
                Session["OperatorDCode"] = (string)Dr["部门编码"];
                Session["OperatorPower"] = (string)Dr["权限"];
                Session["OperatorFunction"] = (string)Dr["功能"];
                Session["OPP2"] = (string)Dr["产品权限"];
                Session["OPP3"] = (string)Dr["原料权限"];
                Session["ImgCode"] = "";
                if (refer != null && refer.Length > 0)
                {
                    Response.Write(
"<script>"
+ "parent.dialog_login_hide();"
+ "parent.document.getElementById('adesktop').innerHTML='" + (string)Dr["姓名"] + "';"
+ "document.URL='"+refer+"';"
+ "</script>"
                    );
                }
                Session["ImgCode"] = "";
            }
            else
            {
                Response.Write("<script>parent.set_dialog_login_error('·用户名或者密码错误!');</script>");
            }
            Dr.Close();
            Dr.Dispose();
            Cmd.Dispose();
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();
        }
    }
}
